package wh.FloatingViewGroupRunner;

import java.util.ArrayList;
import java.util.HashMap;

import Yki.PaperPlaneClient.R;
import Yki.PaperPlaneClient.Friends.Friend;
import Yki.PaperPlaneClient.Friends.FriendCreater;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsoluteLayout;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class TestViewsAdapter extends BaseAdapter {
	private ArrayList<Friend> friends;
	static HashMap<Integer, Integer> indexToId  = new HashMap<Integer, Integer>();

	public TestViewsAdapter() {
		// TODO Auto-generated constructor stub
		initFriends();
	}
	

	// for testing
	private void initFriends() {
		FriendCreater.createFriends();
		friends = FriendCreater.getFriends();
		// put the map from index to id in indexToId
		for(int i = 0; i < friends.size(); i++) {
			indexToId.put(i, friends.get(i).getId());
		}
	}
	
	public static HashMap<Integer,Integer> getIndexToId() {
		return indexToId;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return friends.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, final ViewGroup parent) {
		// TODO Auto-generated method stub
		Context context = parent.getContext();
		
    	AbsoluteLayout absoluteLayout = new AbsoluteLayout(context);
    	

		ImageView imageView=new ImageView(context);
		imageView.setImageResource(R.drawable.friends_portrait);
		AbsoluteLayout.LayoutParams portraitLayoutParams = 
			new AbsoluteLayout.LayoutParams(98, 84, 0, 0);
		absoluteLayout.addView(imageView, portraitLayoutParams);

		ImageView imageViewPortrait = new ImageView(context);
		imageViewPortrait.setImageResource(
				friends.get(position).getPortraitResource());
		AbsoluteLayout.LayoutParams portraitLayoutParamsPortrait = 
			new AbsoluteLayout.LayoutParams(56, 56, 0, 0);
		absoluteLayout.addView(imageViewPortrait, portraitLayoutParamsPortrait);
		
		String name = friends.get(position).getName();
		int nameWidth = name.length()*23 + 23; 
		TextView textView = new TextView(context);
		textView.setText(name);
		
        Typeface typeface =
        	Typeface.createFromAsset(context.getAssets(), "minishanhu.ttf");
		textView.setTypeface(typeface);
		textView.setTextSize(23);
		textView.setTextColor(Color.BLACK);
		AbsoluteLayout.LayoutParams nameLayoutParams = 
			new AbsoluteLayout.LayoutParams( nameWidth, 84, 76, 17);
		absoluteLayout.addView(textView, nameLayoutParams);
		
    	return absoluteLayout;
		
	}

}
